package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.BathroomDetail2Bean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.VideoBean;
import com.wnhz.work.yz.fragment.bathroomdetails.InstallExplainFragment2;
import com.wnhz.work.yz.fragment.bathroomdetails.ParameterFragment2;
import com.wnhz.work.yz.fragment.bathroomdetails.ProbablyFragment2;
import com.wnhz.work.yz.fragment.bathroomdetails.ProgressFragment;
import com.wnhz.work.yz.fragment.bathroomdetails.VideoFragment2;
import com.wnhz.work.yz.utils.LogUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YuShouAllDetailssActivity extends BaseActivity implements View.OnClickListener {
    private ProbablyFragment2 fragment1;
    private ParameterFragment2 fragment2;
    private VideoFragment2 fragment3;
    private InstallExplainFragment2 fragment4;
    private ProgressFragment fragment5;
    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private TextView text1, text2, text3, text4, text5;

    private int textColorNor, textColorCli;
    private View line1, line2, line3, line4, line5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yu_shou_all_detailss);
        EventBus.getDefault().register(this);
     //   MyApplication.getInstance().addDestoryActivity(this, "all_yushou");
        initData();
        initTitle();
        initView();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }
    private void initView() {
        text1 = (TextView) findViewById(R.id.tv_probably);
        text2 = (TextView) findViewById(R.id.tv_parameter);
        text3 = (TextView) findViewById(R.id.tv_video);
        text4 = (TextView) findViewById(R.id.tv_install);
        text5 = (TextView) findViewById(R.id.tv_progress);

        line1 = findViewById(R.id.line1);
        line2 = findViewById(R.id.line2);
        line3 = findViewById(R.id.line3);
        line4 = findViewById(R.id.line4);
        line5 = findViewById(R.id.line5);


        fragmentList = new ArrayList<>();
        fragment1 = new ProbablyFragment2();
        fragment2 = new ParameterFragment2();
        fragment3 = new VideoFragment2();
        fragment4 = new InstallExplainFragment2();
        fragment5 = new ProgressFragment();

        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment3);
        fragmentList.add(fragment4);
        fragmentList.add(fragment5);

        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fl_bathRoom, fragment1);
        transaction.add(R.id.fl_bathRoom, fragment2);
        transaction.add(R.id.fl_bathRoom, fragment3);
        transaction.add(R.id.fl_bathRoom, fragment4);
        transaction.add(R.id.fl_bathRoom, fragment5);

        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
        transaction.hide(fragment5);
        transaction.commit();
        upload();
    }

    private String type, goods_id, name;
    private int isisisyushou = 0;
    private String sku_id, budget_id;
    private String status;
    private String nums;

    private void initData() {
        status = getIntent().getStringExtra("status");
        if (status != null) {
            nums = getIntent().getStringExtra("num");
        }
        isisisyushou = getIntent().getIntExtra("isisisyushou", 0);
        if (isisisyushou != 0) {
            sku_id = getIntent().getStringExtra("sku_id");
            budget_id = getIntent().getStringExtra("budget_id");
        }
        textColorNor = getResources().getColor(R.color.text_two);
        textColorCli = getResources().getColor(R.color.main_color);
        type = getIntent().getStringExtra("type");
        goods_id = getIntent().getStringExtra("goods_id");
        name = getIntent().getStringExtra("name");
    }

    private void hideFragment(int index) {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        for (int i = 0; i < fragmentList.size(); i++) {
            if (i == index) {
                transaction.show(fragmentList.get(i));
            } else {
                transaction.hide(fragmentList.get(i));
            }
        }
        transaction.commit();
    }


    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("" + name);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setOnClickListener(this);
        if ("1".equals(status) || status == null)
            submit.setVisibility(View.VISIBLE);
        submit.setText("下一步");

    }

    private List<BathroomDetail2Bean> beanList = new ArrayList<>();
    private List<VideoBean> videoBeanList = new ArrayList<>();
    private BathroomDetail2Bean bean = new BathroomDetail2Bean();

    private void upload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("goods_id", goods_id);
        map.put("type", type);
        // MyToast(MyApplication.getInstance().getUserBean().getToken());
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.BOOKING_GETBOOKINGPACKAGEDETAIL, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                //   Log.i(TAG, "onSuccess: packageId==== " + result);
                LogUtil.e("-----", result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    beanList.clear();
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        JSONObject jsonObject1 = jsonObject.optJSONObject("info");
                        bean = gson.fromJson(jsonObject.optString("info"), BathroomDetail2Bean.class);
                        beanList.add(bean);
                        fragment1.setBean(bean);
                        fragment4.setData(jsonObject1.optString("explain"));
                        JSONArray j = jsonObject1.optJSONArray("video");
                        videoBeanList.clear();
                        for (int i = 0; i < j.length(); i++) {
                            JSONObject j1 = j.getJSONObject(i);
                            VideoBean videoBean = gson.fromJson(j1.toString(), VideoBean.class);
                            videoBeanList.add(videoBean);
                        }
                    } else {
                        String info = jsonObject.optString("info");
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
                fragment5.setProduct_at(bean.getProduct_at());
                fragment5.setExpress_at(bean.getExpress_at());
                fragment2.setBeanList(beanList);
                fragment3.setData(videoBeanList);
                text1.setOnClickListener(YuShouAllDetailssActivity.this);
                text2.setOnClickListener(YuShouAllDetailssActivity.this);
                text3.setOnClickListener(YuShouAllDetailssActivity.this);
                text4.setOnClickListener(YuShouAllDetailssActivity.this);
                text5.setOnClickListener(YuShouAllDetailssActivity.this);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                if (MyApplication.getInstance().getUserBean() == null) {
                    Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
                    Intent intent2 = new Intent(this, LoginNewActivity.class);
                    startActivity(intent2);
                    return;
                }
                if (isisisyushou != 0) {
                    Intent intent = new Intent(this, YuShouADUActivity.class);
                    intent.putExtra("sku_id", sku_id);
                    intent.putExtra("goods_id", goods_id);
                    intent.putExtra("budget_id", budget_id);
                    intent.putExtra("nums",nums);
                    startActivity(intent);
                } else
                    startActivity(new Intent(this, YuShouDetailssActivity.class).putExtra("goods_id", goods_id).putExtra("type", type).putExtra("name", name));
                break;

            //概括
            case R.id.tv_probably:
                hideFragment(0);
                text1.setTextColor(textColorCli);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorNor);
                text5.setTextColor(textColorNor);

                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                line5.setVisibility(View.INVISIBLE);
                break;

            //参数
            case R.id.tv_parameter:
                hideFragment(1);
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorCli);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorNor);
                text5.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                line5.setVisibility(View.INVISIBLE);

                break;

            //视频
            case R.id.tv_video:
                hideFragment(2);
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorCli);
                text4.setTextColor(textColorNor);
                text5.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.VISIBLE);
                line4.setVisibility(View.INVISIBLE);
                line5.setVisibility(View.INVISIBLE);
                break;

            //安装说明
            case R.id.tv_install:
                hideFragment(3);
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorCli);
                text5.setTextColor(textColorNor);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.VISIBLE);
                line5.setVisibility(View.INVISIBLE);
                break;
            case R.id.tv_progress:
                hideFragment(4);
                text1.setTextColor(textColorNor);
                text2.setTextColor(textColorNor);
                text3.setTextColor(textColorNor);
                text4.setTextColor(textColorNor);
                text5.setTextColor(textColorCli);

                line1.setVisibility(View.INVISIBLE);
                line2.setVisibility(View.INVISIBLE);
                line3.setVisibility(View.INVISIBLE);
                line4.setVisibility(View.INVISIBLE);
                line5.setVisibility(View.VISIBLE);
                break;

        }
    }
}
